const { resolve } = require('path');

function calculateMemory(memory) {
  let count = 0;
  let memory_unit = ['B', 'KB', 'MB', 'GB', 'TB']
  while (memory > 1024 && count < memory_unit.length) {
    memory /= 1024;
    ++count;
  }
  return `${memory.toFixed(2)}${memory_unit[count]}`;
}

window.addEventListener('DOMContentLoaded', () => {
    const ffi = require('ffi-napi')
    const ref = require('ref-napi')
    // 读取动态库接口
    const pingpong_dll = ffi.Library('./pingpong.dll', {
        'Start' : [ref.types.void, [ref.types.int, ref.types.int]],
        'Stop' : [ref.types.void, []],
        'Result' : [ref.types.ulonglong, []]
    });
    // 按钮触发测试
    document.getElementById("start-test-button").onclick = function() {
      // 防止重入
      this.style.pointerEvents = "none";
      // 测试时间
      let timeout = Number(document.getElementById("test-time-number").value) * 1000;
      // 进度条
      let value = 0;
      let count = 0;
      let interval = 200;
      document.getElementById("test-progress").textContent = "0%";
      document.getElementById("test-progress").style.width = "0%";
      document.getElementById("test-progress").classList.add("progress-bar");
      document.getElementById("test-progress").classList.remove("progress-bar-success");
      let progressIntervalId = setInterval(() => {
        ++count;
        value = (interval * count / (timeout * 4) * 100).toFixed(0);
        if (value < 100) {
          document.getElementById("test-progress").textContent = `${value}%`;
          document.getElementById("test-progress").style.width = `${value}%`;
        } else {
          document.getElementById("test-progress").classList.add("progress-bar-success");
          document.getElementById("test-progress").classList.remove("progress-bar");
          document.getElementById("test-progress").textContent = `${value}%`;
          document.getElementById("test-progress").style.width = `${value}%`;
          clearInterval(progressIntervalId);
        }
      }, interval);
      // 重置结果
      document.getElementById("asio-server-asio-client").textContent = "";
      document.getElementById("qt-server-asio-client").textContent = "";
      document.getElementById("asio-server-qt-client").textContent = "";
      document.getElementById("qt-server-qt-client").textContent = "";
      // 第一次测试启动
      pingpong_dll.Start(0, 0);
      let promise = new Promise((resolve) => {setTimeout(() => {
        resolve();
      }, timeout);}).then(() => {
        pingpong_dll.Stop();
        document.getElementById("asio-server-asio-client").textContent = pingpong_dll.Result();
        // 第二次测试启动
        pingpong_dll.Start(0, 1);
        return new Promise((resolve) => {setTimeout(() => {
          resolve();
        }, timeout);});
      }).then(() => {
        pingpong_dll.Stop();
        document.getElementById("qt-server-asio-client").textContent = pingpong_dll.Result();
        // 第三次启动测试
        pingpong_dll.Start(1, 0);
        return new Promise((resolve) => {setTimeout(() => {
          resolve();
        }, timeout);});
      }).then(() => {
        pingpong_dll.Stop();
        document.getElementById("asio-server-qt-client").textContent = pingpong_dll.Result();
        // 第四次启动测试
        pingpong_dll.Start(1, 1);
        return new Promise((resolve) => {setTimeout(() => {
          resolve();
        }, timeout);});
      }).then(() => {
        pingpong_dll.Stop();
        document.getElementById("qt-server-qt-client").textContent = pingpong_dll.Result();
        document.getElementById("start-test-button").style.pointerEvents = "";
      })
    };
    // 系统信息
    let os = require("os");
    document.getElementById("system_version").textContent = os.version();
    document.getElementById("cpu_information").textContent = os.cpus()[0].model;
    document.getElementById("system_memory").textContent = calculateMemory(os.totalmem())
    // 页面切换
    document.getElementById("settings-page-view").style.display = "none";
    document.getElementById("main-page").onclick = function() {
      document.getElementById("main-page").parentNode.classList.add("active");
      document.getElementById("settings-page").parentNode.classList.remove("active");
      document.getElementById("main-page-view").style.display = "";
      document.getElementById("settings-page-view").style.display = "none";
    }
    document.getElementById("settings-page").onclick = function() {
      document.getElementById("main-page").parentNode.classList.remove("active");
      document.getElementById("settings-page").parentNode.classList.add("active");
      document.getElementById("main-page-view").style.display = "none";
      document.getElementById("settings-page-view").style.display = "";
    }
    // 设定默认值
    document.getElementById("test-time-number").value = "5";
    document.getElementById("send-data-text").value = "hello world";
  })